package com.sskj.protocol.sskjboard.model;

import lombok.Data;

@Data
public class FrameMessage {

    private byte commandCode;

    private byte commandType;

    private byte[] data;

    public byte[] buildFrame() {
        if (data == null || data.length == 0) {
            return new byte[]{commandCode, commandType};
        }

        int dataLength = data.length;
        if (dataLength > 0xFFFF) {
            throw new IllegalArgumentException("Data length exceeds maximum 65535 bytes: " + dataLength);
        }

        byte[] frame = new byte[4 + dataLength];

        // 写入 commandCode 和 commandType
        frame [0] = commandCode;
        frame [1] = commandType;

        // 写入 data 长度（2字节，大端序：高字节在前）
        frame[2] = (byte) ((dataLength >> 8) & 0xFF); // 高字节
        frame[3] = (byte) (dataLength & 0xFF);

        System.arraycopy(data, 0, frame, 4,data.length);

        return frame;
    }
}
